package com.chinatelecom.ctyun.xstore.vod.demo.controller;

import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.chinatelecom.ctyun.xstore.vod.demo.client.XstoreVodClient;
import com.ctyun.xstore.vod.video.model.CreateVideoCompleteRequest;
import com.ctyun.xstore.vod.video.model.CreateVideoRequest;
import com.ctyun.xstore.vod.video.model.SearchVideoRequest;

@RestController
@RequestMapping(value = "/video")
public class VideoDemoController {

	@Autowired
	private XstoreVodClient xstoreVodClient;

	/**
	 * 初始化分片上传，获取各片的List<etags>
	 * 
	 * @param videoFile
	 * @param regionCode
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "/create", consumes = "application/json")
	public Object createVideo(@RequestBody JSONObject paramsObject) throws IOException {
		CreateVideoRequest request = paramsObject.toJavaObject(CreateVideoRequest.class);
		return xstoreVodClient.getVideoClient().createVideo(request);
	}

	/**
	 * 上传完成合并分片，更改数据库状态
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "/complete", consumes = "application/json")
	public Object completeUpload(@RequestBody JSONObject paramsObject) throws IOException {
		CreateVideoCompleteRequest request = paramsObject.toJavaObject(CreateVideoCompleteRequest.class);
		return xstoreVodClient.getVideoClient().createVideoComplete(request);
	}

	/**
	 * 搜索源视频相关信息
	 * 
	 * @param paramsObject
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "/search", consumes = "application/json")
	public Object searchVideo(@RequestBody JSONObject paramsObject) throws IOException {
		SearchVideoRequest request = paramsObject.toJavaObject(SearchVideoRequest.class);
		return xstoreVodClient.getVideoClient().searchVideo(request);
	}
}
